<?php


namespace database\model;

use App\Units;
use Illuminate\Database\Schema\Blueprint;

/**
 * Created by PhpStorm.
 * User: ybjaa
 * Date: 2017/6/15
 * Time: 23:01
 */
abstract class base
{

    static private $ArrayBusinesses;

    /**
     * @return array 获得所有基于此类所继承的子类的对象
     */
    static public function GetAllModels()
    {

        if(self::$ArrayBusinesses !=null && count(self::$ArrayBusinesses)>0)
        {
            return self::$ArrayBusinesses;
        }

        $array=Units::getFilePath(__DIR__);
        $prefix=(new \ReflectionClass(base::class))->getNamespaceName();


        //映射为ReflectionClass对象
        $array=array_map(function ($a) use ($prefix) {
            return new \ReflectionClass($prefix.str_replace("//","\\",str_replace([__DIR__,".php"],"",$a)));
        },$array);

        //判断是否是子类以及非抽象类
        $array=array_filter($array,function(\ReflectionClass $a){
            return $a->isSubclassOf(base::class) & !$a->isAbstract() ;
        });


        self::$ArrayBusinesses=array_map(function(\ReflectionClass $a){
            return $a->newInstance();
        },$array);


        return self::$ArrayBusinesses;

    }



    public $table="base";

    protected function setLastFileds(Blueprint $table)
    {
        $table->timestamp('created_at')->nullable()->comment("laravel所创建该条记录的时间");
        $table->timestamp('updated_at')->nullable()->comment("laravel最近一次修改的时间");
        $table->timestamp("deleted_at")->nullable()->comment("软删除");
    }

    protected function setFileds(Blueprint $table)
    {
        $table->increments('ID')->comment("主键");
    }


    public function CreateFileds(Blueprint $table)
    {
        $this->setFileds($table);
        $this->setLastFileds($table);
        $this->setIndex($table);
    }

    protected function UserID(Blueprint $table)
    {
        $table->integer("userID",false,true)->nullable()->comment("用户的ID");
    }

    protected function setIndex(Blueprint $table)
    {
        return;
    }


}